package yygh.order.Service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import yygh.hospital.enums.RefundStatusEnum;
import yygh.hospital.model.order.PaymentInfo;
import yygh.hospital.model.order.RefundInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yygh.order.Mapper.RefundMapper;
import yygh.order.Service.RefundInfoService;

import java.util.Date;

@Service
public class RefundInfoServiceImp extends ServiceImpl<RefundMapper, RefundInfo> implements RefundInfoService {

    @Autowired
    RefundMapper refundMapper;

    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {

        //首先判断当前paymentInfo是否为重复的记录
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("order_id",paymentInfo.getOrderId());
        queryWrapper.eq("payment_type",paymentInfo.getPaymentType());
        RefundInfo refundInfo = refundMapper.selectOne(queryWrapper);
        //如果退款记录表中已经有退款记录，则直接返回refundInfo
        if (refundInfo!=null){
            return  refundInfo;
        }
        //生成一个退款记录表，并将paymentInfo相关参数注入
        refundInfo=new RefundInfo();
        refundInfo.setCreateTime(new Date());
        refundInfo.setOrderId(paymentInfo.getOrderId());
        refundInfo.setPaymentType(paymentInfo.getPaymentType());
        refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
        refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());
        refundInfo.setSubject(paymentInfo.getSubject());
        refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
        baseMapper.insert(refundInfo);
        return refundInfo;
    }
}
